/**

 * User: seeker910
 * Date: 13-10-24
 * Time: 下午10:18
@description page 容器
 */ 
Rsd.define('Rsd.container.PageContainer', {
    extend: 'Rsd.container.Component',
    requires:[ 
        'Rsd.container.Page'
    ],
    xtype: 'page-container',
    layout: 'fit',
    cls:'x-page-container',
    bodyCls:'x-c-body',
    muliti: true,
    currentPage: null,
    /**
     * pages mapping容器
     */
    pages: {},
    /**
     * page 对象 就是 item
     */
    //items: [],
    /**
     * 
     * @param {*} config 
     */
    constructor: function PageContainer(config) {
        config = config || {};
        this.apply(config);
    },
    /*
     * */
    contain: function contain(id) {
        var me = this;
        return me.pages.hasOwnProperty(id) && me.get(id) != null;
    },
    /*
     * */
    get: function get(id) {
        var me = this;
        return me.pages[id];
    },
    /*
     * 由select 控制页面展示 
    */
    select: function select(id,speed) {

        var me = this;
        var _p = me.pages[id]; 
        
        if (_p == null)
        {
            console.error( new Error('page not found:' + id));
            return;
        }
        
        if(_p == me.currentPage)
        {
            return;
        }
        for(var i in this.pages)
        {  
             if(i != id)
             {
                 this.pages[i].hide();
             }
        } 
        _p.select(speed);
        me.currentPage = _p;

    },
    /** 
     * @description 添加到items中， 并显示(show)
    */
    add: function add(page) {
        
        if(page instanceof Rsd.container.Page)
        {}
        else
        {
            console.error('page is not instance of Rsd.container.TPage',page);
            return null;
        }

        var me = this;
 
        if (me.muliti) {
            //多页面
            if(!me.contain(page.id))
            {
                me.callParent(page);
                me.pages[page.id] = page; 
            }
            //根据页面hidden 状态 ，控制添加后是否展示
            if(page.hidden)
            {
               //新赠页面，状态不展示
            } 
            else
            {
                if (me.currentPage) {
                    me.currentPage.hide(); 
                    me.currentPage = null;
                }
                 me.currentPage = page;
                 page.show();
            }
            
        } 
        else 
        {
            //单页面
            if(me.pages.hasOwnProperty(page.id))
            {
                delete  me.pages[page.id];
            }

            if (me.currentPage) {
                me.remove(me.currentPage.id);
                me.currentPage = null;
            }

            me.removeAll(); 
            me.callParent(page); 
            me.pages[page.id] = page;
            //单页面直接添加 立即显示
            me.currentPage = page;
            page.show();
        }
 
        return page;
    },
    /*
     * */
    remove: function remove(id) {
        var me = this; 

        if (me.pages[id]) {
            me.pages[id].close();
            if(me.pages[id] == me.currentPage)
            {
                me.currentPage = null;
            }
            me.removeItem(me.pages[id]);
            delete me.pages[id];
        }
    }
});
